﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Globalization;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator that returns true if value is between to points
    /// </summary>
    public class BetweenValidator<T> : BaseValidator where T : IComparable<T>
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value is not between the min and max, inclusively
        /// </summary>
        private const string NOT_BETWEEN = "notBetween";

        /// <summary>
        /// Validation failure message key for when the value is not strictly between the min and max
        /// </summary>
        private const string NOT_BETWEEN_STRICT = "notBetweenStrict";

        /// <summary>
        /// Validation failure message key for when the value is not the right type
        /// </summary>
        private const string NOT_TYPE = "notType";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {BetweenValidator<T>.NOT_BETWEEN,"'%value%' is not between '%min%' and '%max%', inclusively"},
        {BetweenValidator<T>.NOT_BETWEEN_STRICT,"'%value%' is not strictly between '%min%' and '%max%'"},
        {BetweenValidator<T>.NOT_TYPE,"'%value%' is not type of '%inputtype%'"},
        };

        #endregion

        #region properties
        /// <summary>
        /// Minimum value
        /// </summary>
        public T Min { get; set; }

        /// <summary>
        /// Maximun value
        /// </summary>
        public T Max { get; set; }

        /// <summary>
        /// Whether to do inclusive comparisons, allowing equivalence to min and/or max
        /// </summary>
        public bool Inclusive { get; set; }

        /// <summary>
        /// Input type
        /// </summary>
        public string InputType { get { return typeof(T).Name; } }
        #endregion

        #region constructor
        public BetweenValidator(T min, T max)
            : this(min, max, true)
        {

        }
        public BetweenValidator(T min, T max, bool inclusive)
            : base()
        {
            Min = min;
            Max = max;
            Inclusive = inclusive;
        }
        #endregion

        #region protected methods
        /// <summary>
        /// Returns true if value is between min and max
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool CheckValid(object value)
        {
            rememberValue(value);

            T convertedValue = default(T);

            try
            {
                convertedValue = (T)Convert.ChangeType(value, typeof(T), CultureInfo.CurrentCulture.NumberFormat);
            }
            catch
            {
                raiseMessage(BetweenValidator<T>.NOT_TYPE);
                return false;
            }

            if (Inclusive)
            {
                if (Min.CompareTo(convertedValue) > 0 || convertedValue.CompareTo(Max) > 0)
                {
                    raiseMessage(BetweenValidator<T>.NOT_BETWEEN);
                    return false;
                }
            }
            else
                if (Min.CompareTo(convertedValue) >= 0 || convertedValue.CompareTo(Max) >= 0)
                {
                    raiseMessage(BetweenValidator<T>.NOT_BETWEEN_STRICT);
                    return false;
                }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
